package cdu.javaendwork.Controller.Admin.User;

import cdu.javaendwork.model.sqlModel.Concern;
import cdu.javaendwork.model.sqlModel.User;
import cdu.javaendwork.model.utilModel.PII;
import cdu.javaendwork.service.Impl.User.ConcernServiceImpl;
import cdu.javaendwork.service.Impl.User.UserServiceImpl;
import cdu.javaendwork.service.Interface.User.ConcernService;
import cdu.javaendwork.service.Interface.User.UserService;
import cdu.javaendwork.util.pageHelp.PageHelper;
import cdu.javaendwork.util.pageHelp.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/my_concern")
public class MyConcern extends HttpServlet {
    ConcernService concernService = new ConcernServiceImpl();
    UserService userService = new UserServiceImpl();
    PageHelper pageHelper = new PageHelper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sPage=req.getParameter("page");
        int pageSize=3;
        int pageIndex=(sPage == null || sPage.equals("")) ? 1 : Integer.parseInt(sPage);
        List<User> adminList=userService.AdminLimit(pageIndex,pageSize);
        //拿到登录用户的id
        HttpSession session = req.getSession();
        User user= (User)session.getAttribute("user");
        int userid = user.getId();

        //查询我关注了谁
        List<Concern> concernList = concernService.concernLimit(userid,pageIndex,pageSize);
        List<User> userList = new ArrayList<>();
        List<PII> fansList = new ArrayList<>();
        for(Concern concern : concernList)
        {
            int concernid = concern.getConcernid();
            User tuser = userService.findById(concernid);
            userList.add(tuser);
            int fans = concernService.concernedCnt(tuser.getId());
            fansList.add(new PII(tuser.getId(),fans));
        }

        System.out.println("fanslist:"+fansList);
        System.out.println("userList:"+userList);

        PageInfo pageInfo = pageHelper.filter("Concern","userid="+userid,pageIndex,pageSize);
        System.out.println("pageInfo:"+pageInfo);
        req.setAttribute("userList" ,userList );
        req.setAttribute("pageInfo",pageInfo);
        req.setAttribute("fansList",fansList);
        req.getRequestDispatcher("myConcern.jsp").forward(req,resp);
    }
}
